//
//  UIImage+QSExtension.m
//  TestTouchEvent
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 MGCoin. All rights reserved.
//

#import "UIImage+QSExtension.h"

@implementation UIImage (QSExtension)
#pragma mark - 获取圆形图片
- (instancetype)circleImage
{
    // 开启图形上下文
    UIGraphicsBeginImageContext(self.size);
    
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 矩形框
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    
    // 添加一个圆
    CGContextAddEllipseInRect(ctx, rect);
    
    // 裁剪(裁剪成刚才添加的图形形状)
    CGContextClip(ctx);
    
    // 往圆上面画一张图片
    [self drawInRect:rect];
    
    // 获得上下文中的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    return image;
}

+ (instancetype)circleImageNamed:(NSString *)name
{
    return [[self imageNamed:name] circleImage];
}

#pragma mark - 获取带边框的圆形图片
- (instancetype)circleImageWithBorderWith:(CGFloat)borderWith andBorderColor:(UIColor *)borderColor
{
    // 开启图形上下文
    UIGraphicsBeginImageContext(self.size);
    
    // 获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 矩形框
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    
    /**
     *  1、绘画大圆
     */
    CGRect bigRect = CGRectMake(rect.origin.x + borderWith, rect.origin.y + borderWith, rect.size.width - borderWith * 2, rect.size.height - borderWith * 2);
    CGContextSetLineWidth(ctx, borderWith);
    CGContextAddEllipseInRect(ctx, bigRect);
    [borderColor set];
    CGContextStrokePath(ctx);
    
    /**
     *  2、绘画小圆，设置为裁剪区域
     */
    CGRect smallRect = CGRectMake(rect.origin.x + borderWith, rect.origin.y + borderWith, rect.size.width - 2 * borderWith, rect.size.height -2 * borderWith);
    CGContextAddEllipseInRect(ctx, smallRect);
    CGContextClip(ctx);
    // 设置图片的裁剪区域为小圆
    [self drawInRect:smallRect];
    // 将上下文的内容渲染到视图的layer图层上
    CGContextStrokePath(ctx);
    
    // 获得上下文中的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    return image;
}

+ (instancetype)circleImageNamed:(NSString *)name withBorderWith:(CGFloat)borderWith andBorderColor:(UIColor *)borderColor
{
    return [[self imageNamed:name] circleImageWithBorderWith:borderWith andBorderColor:borderColor];
}

/**
 *  生成一张纯色的图片
 */
+ (instancetype)createImageWithColor:(UIColor*)color width:(CGFloat)width andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, width, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

/**
 *  按比例缩放图片
 */
-(UIImage *)imageCompressFromSize:(CGSize)sourceSize targetSize:(CGSize)targetSize
{
    UIImage *newImage = nil;
    CGSize imageSize = sourceSize;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if(CGSizeEqualToSize(imageSize, targetSize) == NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [self drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage;
}
@end
